package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.ExperDao;
import cn.sumpu.app.guoguojie.domain.MemberLearning;
import cn.sumpu.app.guoguojie.domain.MemberWork;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class ExperDaoImpl extends SqlSessionDaoSupport implements ExperDao {

	private static Logger logger = LoggerFactory.getLogger(ExperDaoImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberLearning> getLearningList(String member)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.ExperMapper.getLearningList",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query getLearningList failure.", e);
			}
			throw new AppBusinessException("query getLearningList failure.", e);
		}
	}

	@Override
	public MemberLearning getLearningById(String id)
			throws AppBusinessException {
		try {
			return (MemberLearning) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.ExperMapper.getLearningById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query getLearningById failure.", e);
			}
			throw new AppBusinessException("query getLearningById failure.", e);
		}
	}

	@Override
	public void insertLearning(MemberLearning learing)
			throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.ExperMapper.insertLearning",
							learing);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertLearning failure.", e);
			}
			throw new AppBusinessException("insertLearning failure.", e);
		}
	}

	@Override
	public void deleteLearning(String member) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ExperMapper.deleteLearning",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteLearning failure.", e);
			}
			throw new AppBusinessException("deleteLearning failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MemberWork> getWorkList(String member)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.ExperMapper.getWorkList",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query getWorkList failure.", e);
			}
			throw new AppBusinessException("query getWorkList failure.", e);
		}
	}

	@Override
	public void insertWork(MemberWork work) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("cn.sumpu.app.guoguojie.persistence.ExperMapper.insertWork",
							work);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertWork failure.", e);
			}
			throw new AppBusinessException("insertWork failure.", e);
		}
	}

	@Override
	public void deleteWork(String member) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ExperMapper.deleteWork",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteWork failure.", e);
			}
			throw new AppBusinessException("deleteWork failure.", e);
		}
	}

	@Override
	public void updateLearning(MemberLearning learning)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.ExperMapper.updateLearning",
							learning);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateLearning failure.", e);
			}
			throw new AppBusinessException("updateLearning failure.", e);
		}
	}

	@Override
	public void deleteLearningById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ExperMapper.deleteLearningById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteLearningById failure.", e);
			}
			throw new AppBusinessException("deleteLearningById failure.", e);
		}

	}

	@Override
	public void updateWork(MemberWork work) throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.ExperMapper.updateWork",
							work);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateWork failure.", e);
			}
			throw new AppBusinessException("updateWork failure.", e);
		}

	}

	@Override
	public void deleteWorkById(String id) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.ExperMapper.deleteWorkById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteWorkById failure.", e);
			}
			throw new AppBusinessException("deleteWorkById failure.", e);
		}

	}

	@Override
	public MemberWork getWorkById(String id) throws AppBusinessException {
		try {
			return (MemberWork) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.ExperMapper.getWorkById",
							id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getWorkById failure.", e);
			}
			throw new AppBusinessException("getWorkById failure.", e);
		}
	}

}
